﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using EnvDTE80;

namespace BovespaDesignRules
{
    public sealed class Rule_ClassMustHaveDefaultConstructor : BaseAddIn.Rules.BaseRuleAddIn
    {
        #region Constructor

        public Rule_ClassMustHaveDefaultConstructor()
            : base("BovespaClassMustHaveDefaultConstructor", "BovespaDesignRules.BovespaDesignRules", typeof(Rule_ClassMustHaveDefaultConstructor).Assembly)
        {
        }

        #endregion

        #region Protected Override Methods

        protected sealed override void Check(CodeClass2 classe)
        {
            CodeElement2 aux;
            CodeFunction function;
            bool construtorDefaultImplementado = false;
            bool contemConstrutorNaoDefault = false;

            for (int countClassChildren = 1; countClassChildren <= classe.Children.Count; countClassChildren++)
            {
                aux = (CodeElement2)classe.Children.Item(countClassChildren);

                if (aux.Kind == vsCMElement.vsCMElementFunction)
                {
                    function = (CodeFunction)aux;

                    if (function.FunctionKind == vsCMFunction.vsCMFunctionConstructor)
                    {
                        if (function.Parameters == null
                            || function.Parameters.Count == 0)
                        {
                            //O construtor default foi explicitamente implementado,
                            //logo não é necessário continuar a análise
                            construtorDefaultImplementado = true;
                            break;
                        }
                        else
                        {
                            contemConstrutorNaoDefault = true;
                        }
                    }
                }
            }

            if (!construtorDefaultImplementado
                 && contemConstrutorNaoDefault)
            {
                GeraProblemaAddIn(classe.StartPoint.Parent.Parent.FullName,
                                  classe.StartPoint.Line,
                                  classe.StartPoint.DisplayColumn,
                                  "ClassMustHaveDefaultConstructorResolution",
                                  classe.Name,
                                  classe.Namespace.Name);
            }
        }

        #endregion
    }
}
